<?php

namespace App\Admin\Controllers; 

use App\Admin\Models\CustomerModel;

use App\Models\ProjectModel;
use App\Admin\Models\UserModel;
use App\Admin\Models\ManagerModel;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

use Illuminate\Http\Request;


class ManagerController extends Controller
{
    use ModelForm;
    
      public function index(){
        return Admin::content(function (Content $content) {

            $content->header('项目经理');
            $content->description('');
//            $method = isset($_GET['method'])?$_GET['method']:0;
            $content->body($this->grid());
        });
    }
    public function create(){
        return Admin::content(function (Content $content) {

            $content->header('新增经理');
            $content->description('');

            $content->body($this->form());
        });
    }
    
    public function edit(Request $request){
        $this->_id = $request->route('manager');
        return Admin::content(function (Content $content) {

            $content->header('编辑经理');
            $content->description('');
            $content->body($this->form()->edit($this->_id));
        }); 
    }
    
 
    
    public function grid()
    {
//        $this->_method=$method;
        return Admin::grid(ManagerModel::class, function (Grid $grid) {
            
             $grid->resource('admin/manager');

//            $grid->id('ID')->sortable();
//            $grid->column('company','公司名称')->sortable();
//            $grid->column('projectid','项目名称')->value(function($projectId){
//                $oProject = new ProjectModel();
//                $oP = $oProject->getProject($projectId);
//                return $oP->projectname;
//            });
            $grid->column('id','项目名称')->value(function($userId){
                $oManager = new ProjectModel(); 
                $oM = $oManager->getProjectbyManager($userId); 
                if(isset($oM->projectname))
                {
                return $oM->projectname;
                }
                else{
                    return '';
                }
            });
            $grid->column('name','项目经理');
          
 
            $grid->filter(function ($filter) {
    
                  // 设置created_at字段的范围查询
                      $filter->like('name', '经理名称'); 
                     $filter->disableIdFilter();
             });
//             var_dump($this->_method); 
          
             $aMe = Admin::user();
//             var_dump($aMe->id);exit; 
//             $grid->allowEdit();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableBatchDeletion();
//            $grid->disableActions();
        });
    }
    
    protected function form()
    {
        return Admin::form(ManagerModel::class, function (Form $form) {
            $id = Admin::user()->id;
//             $oP = new ProjectModel();
//             $aPs = $oP->getManagerNames($id);
//             $aJ = $oP->getProjectNames();
//                var_dump($aJ);   
            $form->text('username','用户名')->rules('required');   
            $form->text('name','姓名')->rules('required');          
            $form->select('gender','性别')->options(array('1'=>'男','2'=>'女'));
            $form->text('address','户籍')->rules('required');
            $form->text('degree','学历')->rules('required');
            $form->password('password','密码');
            $form->text('worktime','工作年限');
            $form->text('title', '职称');
//            $form->setAction('/admin/projects/save');
//            $form->hidden('assigneeid')->value($id);

//            $form->display('id', 'ID');
 
            

//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
            $form->saving(function(Form $form) {
                if($form->password && $form->model()->password != $form->password)
                {
                    $form->password = bcrypt($form->password);
                }
            });
            
            $form->saved(function (Form $form) {
                 return redirect('/admin/manager');
                 });

        });
          
 
    }
}
